package com.relsell.tmcprototype;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import com.relsell.controller.SlideMainActivityCallback;
import com.relsell.model.ParserImpl;
import com.relsell.utility.ApplicationVars;
import com.relsell.utility.CirclePageIndicator;
import com.relsell.view.phone.ContactUs;
import com.relsell.view.phone.Instructors;
import com.relsell.view.phone.InstructorsFragment;
import com.relsell.view.phone.RecentBollywoodSongs;
import com.relsell.view.phone.RecentGuitarLessons;
import com.relsell.view.phone.RecentSongLessons;

public class ScreenSlidePagerActivity extends FragmentActivity implements
		SlideMainActivityCallback {
	/**
	 * The number of pages (wizard steps) to show in this demo.
	 */
	private static final int NUM_PAGES = 4;

	/**
	 * The pager widget, which handles animation and allows swiping horizontally
	 * to access previous and next wizard steps.
	 */
	private ViewPager mPager;

	/**
	 * The pager adapter, which provides the pages to the view pager widget.
	 */
	private PagerAdapter mPagerAdapter;

	CirclePageIndicator circleIndicator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_slide);
		ApplicationVars.getInstance().setupActionBar(
				ScreenSlidePagerActivity.this, getActionBar(), false, false,
				false, true, true);

		ApplicationVars.getInstance().setEmptySliderCallBack(this);
		// Instantiate a ViewPager and a PagerAdapter.
		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
		mPager.setAdapter(mPagerAdapter);

		circleIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
		circleIndicator.setViewPager(mPager);
		

		// circleIndicator.setOnPageChangeListener(listener;)
	}

	@Override
	public void onBackPressed() {
		if (mPager.getCurrentItem() == 0) {
			// If the user is currently looking at the first step, allow the
			// system to handle the
			// Back button. This calls finish() on this activity and pops the
			// back stack.
			super.onBackPressed();
		} else {
			// Otherwise, select the previous step.
			mPager.setCurrentItem(mPager.getCurrentItem() - 1);
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_contactus:
			startActivity(new Intent(ScreenSlidePagerActivity.this,
					ContactUs.class));
			return true;
		case R.id.action_search:
			ApplicationVars.getInstance().showCustomDialog(
					ScreenSlidePagerActivity.this);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();

	}

	/**
	 * A simple pager adapter that represents 5 ScreenSlidePageFragment objects,
	 * in sequence.
	 */
	private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
		public ScreenSlidePagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			if (position == 0) {
				return new MainActivity();
			}
			if (position == 1) {
				return new RecentBollywoodSongs();
			} else if (position == 2) {
				return new RecentSongLessons();
			} else {
				return new RecentGuitarLessons();
			}
		}

		@Override
		public int getCount() {
			return NUM_PAGES;
		}

	}

	@Override
	public void showInstructorsView() {
		((ViewPager) findViewById(R.id.pager)).setVisibility(View.GONE);
		((CirclePageIndicator) findViewById(R.id.indicator))
				.setVisibility(View.GONE);
		InstructorsFragment ins = new InstructorsFragment();
		FrameLayout fml = (FrameLayout) findViewById(R.id.fragment_container);
		fml.setVisibility(View.VISIBLE);
		getSupportFragmentManager().beginTransaction()
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
				.add(fml.getId(), ins)
				.commit();
	}

}